package filemanager
{
	import events.MyEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	
	import mx.collections.ArrayCollection;
	
	
	
	public class FileScaner extends EventDispatcher
	{
		private var filters:Array=new Array();
		public function FileScaner(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		
		public function scanFolder(_folder:File):Array
		{
			var _arr:Array=new Array();
			
			if (_folder.isDirectory)
			{
				
					
				var arr:Array = _folder.getDirectoryListing();
			
				for each (var _file:File in arr)
				{
				
					var folderObject:FolderClass=new FolderClass(_file.nativePath);
					if (_file.isDirectory)
					{
						
						_arr.push(folderObject);
						folderObject.children=scanFolder(_file);	
						if(folderObject.children.length==0)
						{
							_arr.splice(_arr.length-1,1);
							folderObject=null;
						}
					}
					else
					{
						if(filterFiles(_file))
						{
							_arr.push(folderObject);
						}
						
						
						
					}
				}
								
			}
			_arr.sortOn("isFolder",Array.NUMERIC);  
			return _arr;
		}
		
		
		
		public function set _filters(s:Array):void
		{
			this.filters=s;
			
		}
		
		private function filterFiles(s:File):Boolean
		{
			var ok:Boolean=false;
			var path:String = s.nativePath;
			var startIndex:int = path.lastIndexOf("\\");
			var endIndex:int = path.lastIndexOf(".");
			var str:String=path.substring(endIndex + 1, path.length);
			str= str.toLowerCase();
			if(filters==null)return true;
			if(filters.length>0)
			{
				for(var i:uint=0;i<filters.length;i++)
				{
					var _str:String=filters[i].toLowerCase();
					if(str==_str)
					{
						ok=true;
						break;
					}
				}
			}
			else
			{
				return true;
			}
			return ok;
		}
	}
}